
//
//  HYOUStrategyCollectionView.m
//  BestGift
//
//  Created by 温晓宇 on 15/12/30.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "HYOUStrategyCollectionView.h"
#import "HYOUStrategyCollectionViewCell.h"
#import "HYOUStrategyCollectionModel.h"
#import "HYOUSortDetailViewController.h"
@interface HYOUStrategyCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain) HYOUBaseCollectionView *myCollectionView;
@property(nonatomic, retain) UILabel *nameLabel;

@property(nonatomic, retain) HYOUSortDetailViewController *sotrDetail;


@end

@implementation HYOUStrategyCollectionView
-(void)dealloc{
    [_myCollectionView release];
    [_allArray release];
    [_name release];
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item大小的
//    flowLayout.itemSize = CGSizeMake((WIDTH - 100) / 4, (HEIGHT - 40) / 4);
    flowLayout.itemSize = CGSizeMake(70, 100);
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 15;
    // 设置item与屏幕四周的间距
    // 边界距离:上, 左, 下, 右
//    flowLayout.sectionInset = UIEdgeInsetsMake(40, 5, 10, 10);
    flowLayout.sectionInset = UIEdgeInsetsMake(50, 15, 15, 15);
    //    [flowLayout release];
    //    // 设置最小行间距
    //    flowLayout.minimumLineSpacing = 10;
    //    // 设置最小列间距
    //    flowLayout.minimumInteritemSpacing = 1;
    self.myCollectionView = [[HYOUBaseCollectionView alloc] initWithFrame:CGRectMake(0, 10, WIDTH, (self.allArray.count + 3) / 4 * 115 + 50) collectionViewLayout:flowLayout];
    
    [self.myCollectionView registerClass:[HYOUStrategyCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    
//    self.myCollectionView.backgroundColor = RGBA(242, 242, 242, 1);
//    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self addSubview:self.myCollectionView];
    [self.myCollectionView reloadData];
    [_myCollectionView release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, WIDTH - 100, 30)];
    self.nameLabel.font = [UIFont systemFontOfSize:20];
    [self.myCollectionView addSubview:self.nameLabel];
    

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUStrategyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    cell.model = [self.allArray objectAtIndex:indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUStrategyCollectionModel *model = [self.allArray objectAtIndex:indexPath.row];
    NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?generation=2&gender=2&limit=20&ad=2&offset=0", model.id];
//    [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
//        NSMutableDictionary *bigDic = resuposeObject;
//        NSMutableDictionary *dic = [bigDic objectForKey:@"data"];
//        NSMutableArray *items = [dic objectForKey:@"items"];
//        for (NSDictionary *dic in items) {
//            HYOUOneCellModel *model = [[HYOUOneCellModel alloc]init];;
//            [model setValuesForKeysWithDictionary:dic];
//            [self.collectionArray addObject:model];
//            [model release];
//        }
//            [self.delegate showDetail:self.collectionArray];
//        NSLog(@"%@",model.id);
//    }];
    [self.delegate showDetail:url];
}
-(void)setAllArray:(NSMutableArray *)allArray{
    if (_allArray != allArray) {
        [_allArray release];
        _allArray = [allArray retain];
    }
    self.myCollectionView.frame = CGRectMake(0, 10, WIDTH, (self.allArray.count + 3) / 4 * 115 + 70);
    [self.myCollectionView reloadData];
}
-(void)setName:(HYOUStrategyName *)name{
    if (_name != name) {
        [_name release];
        _name = [name retain];
    }
    self.nameLabel.text = self.name.name;
    [self.myCollectionView reloadData];
}

@end
